/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.report.controller;

import com.tamnhat.entity.Product;
import com.tamnhat.entity.Service;
import com.tamnhat.lazyLoading.LazyAbstractDataModel;
import com.tamnhat.manager.model.ProductModel;
import com.tamnhat.manager.model.ServiceModel;
import com.tamnhat.report.entity.BaocaoPro;
import com.tamnhat.report.entity.Doitac;
import com.tamnhat.report.entity.Sanpham;
import com.tamnhat.report.entity.SUM_SDP_CDR;
import com.tamnhat.report.model.SUM_SDP_CDRModel;
import com.telsoft.applet.util.StringUtil;
import com.tg.smsgw.ws.AppServer;
import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.primefaces.model.LazyDataModel;
import telsoft.admin.util.App;
import telsoft.cms_admin.login.LoginBean;

/**
 *
 * @author Administrator
 */
@ViewScoped
public class BaocaoDoanhthuController implements Serializable {

    private Date tuNgay;
    private Date denNgay;
    private ArrayList<Service> listService = new ArrayList<Service>();
    private ArrayList<Sanpham> listProduct = new ArrayList<Sanpham>();
    private String productId;
    private String serviceId;
    private long total = 0;
    private String doitac;
    private ArrayList<Doitac> listDoitac = new ArrayList<Doitac>();
    private List<String> chitietTheoNgay;
    private LazyDataModel<SUM_SDP_CDR> lazyDataModel;
    private List<SUM_SDP_CDR> listSSC = new ArrayList();
    private boolean is_admin = false;
    /**
     * Creates a new instance of BaocaoDoanhthuController
     */
    public BaocaoDoanhthuController() {
        try {
            Long i = Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.USER_ID).toString());            
            listService = ServiceModel.getAll();
            listProduct = BaocaoPro.listSanpham(i);
            listDoitac = BaocaoPro.listDoitac(i);
            is_admin = (Boolean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.IS_ADMIN);
        } catch (Exception e) {
            is_admin = false;
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
    }

    public boolean isIs_admin() {
        return is_admin;
    }

    public void setIs_admin(boolean is_admin) {
        this.is_admin = is_admin;
    }

    public LazyDataModel<SUM_SDP_CDR> getLazyDataModel() {
        return lazyDataModel;
    }

    public void setLazyDataModel(LazyDataModel<SUM_SDP_CDR> lazyDataModel) {
        this.lazyDataModel = lazyDataModel;
    }

    public List<SUM_SDP_CDR> getListSSC() {
        return listSSC;
    }

    public void setListSSC(List<SUM_SDP_CDR> listSSC) {
        this.listSSC = listSSC;
    }

    public List<String> getChitietTheoNgay() {
        return chitietTheoNgay;
    }

    public void setChitietTheoNgay(List<String> chitietTheoNgay) {
        this.chitietTheoNgay = chitietTheoNgay;
    }

    public String getDoitac() {
        return doitac;
    }

    public void setDoitac(String doitac) {
        this.doitac = doitac;
    }

    public ArrayList<Doitac> getListDoitac() {
        return listDoitac;
    }

    public void setListDoitac(ArrayList<Doitac> listDoitac) {
        this.listDoitac = listDoitac;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public ArrayList<Service> getListService() {
        return listService;
    }

    public void setListService(ArrayList<Service> listService) {
        this.listService = listService;
    }

    public ArrayList<Sanpham> getListProduct() {
        return listProduct;
    }

    public void setListProduct(ArrayList<Sanpham> listProduct) {
        this.listProduct = listProduct;
    }

    public Date getTuNgay() {
        return tuNgay;
    }

    public void setTuNgay(Date tuNgay) {
        this.tuNgay = tuNgay;
    }

    public Date getDenNgay() {
        return denNgay;
    }

    public void setDenNgay(Date denNgay) {
        this.denNgay = denNgay;
    }

    public boolean checkNgay() {
        if (tuNgay == null || denNgay == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Từ ngày hoặc đến ngày nhập không hợp lệ!"));
            return false;
        }
        if (denNgay.compareTo(tuNgay) < 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Đến ngày không được trước từ ngày!"));
            return false;
        }
        return true;
    }

    public String thongke() {
        try {
            total = 0;
            if (checkNgay()) {
                if(productId.equals("")) productId = null;
                serviceId = null;
                if(doitac.equals("")) doitac = null;
                if (chitietTheoNgay.size() == 0) {
                    listSSC = SUM_SDP_CDRModel.getAllByConditionNoDay(StringUtil.format(tuNgay, "dd-MM-yyyy"), StringUtil.format(denNgay, "dd-MM-yyyy"), productId, serviceId, doitac);
                } else {
                    listSSC = SUM_SDP_CDRModel.getAllByConditionByDay(StringUtil.format(tuNgay, "dd-MM-yyyy"), StringUtil.format(denNgay, "dd-MM-yyyy"), productId, serviceId, doitac);
                }                
                lazyDataModel = new LazyAbstractDataModel<SUM_SDP_CDR>(listSSC);
                int i = listSSC.size();
                if (i > 0) {
                    total = total(listSSC);
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có " + i + " bản ghi được tìm thấy!"));
                }
                else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghi nào được tìm thấy!"));
                }
            }
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ERROR: " + e.getMessage()));
            e.printStackTrace();
        }
        return "";
    }

    public long total(List<SUM_SDP_CDR> listTotal) {
        long tong = 0;
        for (int i = 0; i < listTotal.size(); i++) {
            tong += listTotal.get(i).getSum_charge_amount();
        }
        return tong;
    }    
}
